package by.admonko.universaltesting.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import by.admonko.universaltesting.dataaccess.AnswerDao;
import by.admonko.universaltesting.datamodel.Answer;
import by.admonko.universaltesting.datamodel.Answer_;
import by.admonko.universaltesting.datamodel.Question;

@Repository
public class AnswerDaoImpl extends AbstractDaoImpl<Integer, Answer> implements
		AnswerDao {

	protected AnswerDaoImpl() {
		super(Answer.class);
	}

	@Override
	public List<Answer> getByQuestion(Question quest) {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Answer> root = cBuilder.createQuery(Answer.class);
		Root<Answer> criteria = root.from(Answer.class);

		root.select(criteria);

		root.where(cBuilder.equal(criteria.get(Answer_.question), quest));

		TypedQuery<Answer> query = getEm().createQuery(root);
		List<Answer> list = query.getResultList();
		return list;
	}

}
